import functools

from flask import session, current_app, g

from application import db
from application.models import User
dict_room = {}

class RoomFactory:
    def __init__(self):
        pass
    
    
def add_source(id, sou):
    try:
        user = User.query.filter(User.id == id).first()
        user.sources = user.sources + sou
        db.session.commit()
    except Exception as e:
        print(e)
        db.session.rollback()
        
def user_login_data(f):
    @functools.wraps(f)
    def warpper(*args,**kwargs):
        user_id = session.get("id", None)
        user = None
        if user_id:
            try:
                from application.models import User
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        g.user = user
        return f(*args, **kwargs)

    return warpper

    